'''
breif:
    demo for GLM-4-Flash
    This demo is based on the GLM-4-Flash model and the ZhipuAI API,
    This demo provides user a class to chat with GLM-4.
    For more information, please refer to https://bigmodel.cn/dev/api/normal-model/glm-4
'''
from zhipuai import ZhipuAI
import json
content  =  open('API.json','r',encoding='utf-8').read()
api = json.loads(content)['api']
client = ZhipuAI(api_key=api)

class AI():
    def __init__(self):
        ...
    def chat(self,text):
        response = client.chat.completions.create(
        model="glm-4-flash",
        messages=[
            {
                "role": "system",
                "content": "你是一个乐于解答各种问题的助手，你的任务是为用户提供专业、准确、有见地的建议。" 
            },
            {
                "role": "user",
                "content": text
            }
        ],
        top_p= 0.7,
        temperature= 0.95,
        max_tokens=1024,
        tools = [{"type":"web_search","web_search":{"search_result":True}}],
        stream=True
        )
        for trunk in response:
            content = content = trunk.choices[0].delta.content
            print(content, end="")
if __name__ == "__main__":
    ai = AI()
    ai.chat("你好")